package io2;

import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class File3 {

    public static void main(String[] args) throws IOException {
        Scanner scanner = new Scanner(System.in);
        System.out.print("请输入要复制的文件（绝对路径/相对路径）：");

        File sourceFile = new File(scanner.next());
        if(!sourceFile.isDirectory()) {
            System.out.println("路径有误~");
            return;
        }

        System.out.println("请输入要复制的目标路径（绝对路径/相对路径）：");
        File destFile = new File(scanner.next());
        if (destFile.exists()) {
            if (destFile.isDirectory()) {
                System.out.println("目标路径已存在，并且是个目录，请确认路径是否正确");
                return;
            }
            if (destFile.isFile()) {
                System.out.println("路径正确，是否确定要覆盖？ Y/N");
                if (!scanner.next().toLowerCase().equals("y")) {
                    System.out.println("停止复制");
                    return;
                }
            }
        }

        try (InputStream is = new FileInputStream(sourceFile);
             OutputStream os = new FileOutputStream(destFile)) {
            byte[] dest = new byte[4096];
            int len = 0;

            while (true) {
                len = is.read();
                if (len == -1) {
                    break;
                }
                os.write(dest, 0, len);
            }
            os.flush();
        }
        System.out.println("复制已完成");
    }

    public static void main0(String[] args) throws IOException {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入要扫描的根目录（绝对路径/相对路径）:");
        String rootDirPath = scanner.next();

        File rootDir = new File(rootDirPath);
        if (!rootDir.isDirectory()) {
            System.out.println("您输入的根目录不存在或者不是目录，请重试");
            return;
        }

        System.out.print("请输入要找出的文件名中的字符：");
        String token = scanner.next();

        List<File> result = new ArrayList<>();
        scanDir(rootDir, token, result);
        for (File f: result) {
            System.out.println(f.getCanonicalPath() + "请问是否需要删除该文件？ y/n");
            String in = scanner.next();
            if (in.toLowerCase().equals("y")) {
                f.delete();
            }
        }

    }

    //扫描文档,假如是目录就递归，否则查看文件名是否是需求的文件名，是的话就add List
    private static void scanDir (File rootDir, String token, List<File> result) {
        File[] files = rootDir.listFiles();
        if (files == null || files.length == 0) {
            return;
        }

        for (File f :files) {
            if (f.isDirectory()) {
                scanDir(f, token, result);
            } else {
                if (f.getName().contains(token)) {
                    result.add(f.getAbsoluteFile());
                }
            }
        }
    }

}

class myFile {

    public static void main(String[] args) throws IOException {
        Scanner scanner = new Scanner(System.in);
        System.out.print("请输入要删除的文件所在的根目录：");

        File rootDir = new File(scanner.next());
        if (!rootDir.isDirectory()) {
            System.out.println("你输入的根目录不存在或输入错误==");
            return;
        }

        System.out.print("请输入要删除的文件名：");
        String token = scanner.next();

        List<File> result = new ArrayList<>();
        scanDir(rootDir, token, result);

        for (File f: result) {
            System.out.println("找到文件路径为：" + f.getCanonicalPath());
            System.out.println("是否要删除该文件？ Y/N");

            if (scanner.next().toLowerCase().equals("Y")) {
                f.delete();
            }
        }
    }

    private static void scanDir (File rootDir, String token, List<File> list) {
        if (list == null || list.size() == 0)
            return;

        File[] files = rootDir.listFiles();

        for (File f: files) {
            if (f.isDirectory()) {
                scanDir(f, token, list);
            } else {
                if (f.getName().contains(token))
                    list.add(f.getAbsoluteFile());
            }
        }
    }

}
